package com.example.springbootmvc;

import jakarta.servlet.http.Cookie;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.Arrays;
import java.util.List;



/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: Hua YY
 * Date: 2024-11-04
 * Time: 21:25
 */
@RequestMapping("/request")
@RestController
public class RequestController {
    @RequestMapping("/hello")
    public String say(){
        return "hello byte";
    }
    @RequestMapping("/r0")
    public String say1(){
        return "hello byte";
    }
    @RequestMapping("/r1")
    public String r1(String name){
        return name;
    }
    @RequestMapping("/r2")
    public int r2(int age){
        return age;
    }
    @RequestMapping("/r3")
    public String r3(Integer age){
        return "接收到的参数为："+age;
    }
    @RequestMapping("/r3_5")
    public Object r3_5(Person p){
        return p.toString();
    }

    @RequestMapping("r4")
    public Object r4(@RequestParam(value = "time",required = false) String createtime){
        return "接受到参数createtime:" + createtime;
    }

    @RequestMapping("/r5")
    public String r5(String[] arrayParam){
        return Arrays.toString(arrayParam);
    }

    @RequestMapping("/r6")
    public String r6(@RequestParam List<String> listParam){
        return "size:"+listParam.size()+"   "+"listParam:"+listParam;
    }
    @RequestMapping("/r7")
    public Object r7(Person p){
        return p.toString();
    }

    @RequestMapping("/r8")
    public Object r8(@RequestBody Person p){
        return p.toString();
    }

    @RequestMapping("m1/{password}/{name}")
    public String m1(@PathVariable("password") Integer id , @PathVariable("name") String name){
        return "id是：" + id +"    name是：" + name;
    }

    @RequestMapping("m2")
    public String getfile(@RequestPart("fileTemp") MultipartFile file){
        String fileName = file.getOriginalFilename();
        return "接受到的文件的名称为：" + fileName;
    }

    @RequestMapping("m3")
    public String m3(HttpServletRequest request , HttpServletResponse response){
        //获取Cookie信息
        Cookie[] cookies = request.getCookies();
        //打印Cookie信息
        StringBuilder builder = new StringBuilder();
        if(cookies != null){
            for(Cookie ck : cookies){
                builder.append(ck.getName() + ":" + ck.getValue());
            }
        }
        return "返回的Cookie信息是：" + builder;

    }

    @RequestMapping("m3.1")
    public String m3_1(HttpServletRequest request , HttpServletResponse response){
        Cookie[] cookies = request.getCookies();
        if(cookies != null){
            Arrays.stream(cookies).forEach(ck-> System.out.println(ck.getName() + " " + ck.getValue()));
        }
        return "获取Cookie";
    }
    @RequestMapping("m3.2")
    public String m3_2(@CookieValue("bite") String bite){
        return "bite:" + bite;
    }
    @RequestMapping("m4")
    public String m4(HttpServletRequest request){
        HttpSession session = request.getSession();
        if(session != null){
            session.setAttribute("username","java");

        }
        return "session存储成功";
    }
    @RequestMapping("m5")
    public String m5(HttpServletRequest request){
        HttpSession session = request.getSession(false);
        String username = null;
        if(session != null && session.getAttribute("username") != null ){
            username = (String)session.getAttribute("username");

        }
        return "username:" + username;
    }
    @RequestMapping("m7")
    public String m7(HttpSession session){
        String username = (String) session.getAttribute("username");
        return "m7方法中返回的username参数为：" + username;
    }
    @RequestMapping("m6")
    public String m6(@SessionAttribute(value = "username" , required = false) String username){
        return "m6方法中返回的username参数为：" + username;
    }

    //获取header
    @RequestMapping("/param1")
    public String  param1(HttpServletRequest request , HttpServletResponse response){
        String userAgent = request.getHeader("User-Agent");
        return "userAgent "+  ":" + userAgent;
    }

    @RequestMapping("/param2")
    public String para2(@RequestHeader("User-Agent") String userAgent){
        return "userAgent "+  ":" + userAgent;
    }

    @RequestMapping("test")
    public String test(@RequestBody Student student){
        System.out.println();
        return "返回的参数为"+student;
    }




}
